package com.hxy.travel.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.travel.constant.RedisConstant;
import com.hxy.travel.dao.MemberDao;
import com.hxy.travel.dao.OrderDao;
import com.hxy.travel.dao.SetmealDao;
import com.hxy.travel.dao.TravelGroupDao;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.pojo.Setmeal;
import com.hxy.travel.pojo.TravelGroup;
import com.hxy.travel.service.SetmealService;
import com.hxy.travel.service.TravelGroupService;
import com.hxy.travel.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public void addSetmealAndIds(Setmeal setmeal, Integer[] travelGroupIds) {

        setmealDao.addSetmeal(setmeal);

        String img=setmeal.getImg().substring(setmeal.getImg().lastIndexOf("/")+1);

       jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);

        if (travelGroupIds.length > 0){

            for (Integer travelGroupId : travelGroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("travelGroupId",travelGroupId);
                map.put("setmealId",setmeal.getId());

                setmealDao.addSetmealAndItem(map);
            }
        }
    }



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage() == null ? 1 : queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize() == null ? 10 : queryPageBean.getPageSize();

        PageHelper.startPage(currentPage,pageSize);


        List<Setmeal> list = setmealDao.findPage(queryPageBean.getQueryString());

        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Setmeal selectById(Integer id) {
        return setmealDao.selectById(id);
    }

    @Override
    public void editSetmeal(Setmeal setmeal, Integer[] travelgroupIds) {
        setmealDao.editSetmeal(setmeal);

        setmealDao.deleteSetmealAndGroupById(setmeal.getId());

        if (travelgroupIds.length > 0){

            for (Integer travelgroupId : travelgroupIds) {

                Map<String,Integer> map = new HashMap<>();

                map.put("travelgroupId",travelgroupId);

                map.put("setmealId",setmeal.getId());

                setmealDao.addSetmealAndGroup(map);

            }
        }
    }

    @Override
    public List<Setmeal> selectSetmeal() {
        return setmealDao.selectSetmeal();
    }

    @Override
    public Setmeal findById(String id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Map<String, Object>> findSetMealCount() {
        return setmealDao.findSetMealCount();
    }

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap();
        try {
           String today = DateUtils.parseDate2String(DateUtils.getToday());

           String weekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

           String weekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());

           String monthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

           String monthLast = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

           Integer todayNewMember = memberDao.getTodayNewMember(today);

           Integer totalMember = memberDao.getTotalMember();

           Integer thisWeekNewMember = memberDao.getThisWeekNewMember(weekMonday);

           Integer thisMonthNewMember = memberDao.getThisMonthNewMember(monthFirst);

           Integer todayOrderNumber = orderDao.getTodayOrderNumber(today);

           Integer todayVisitsNumber = orderDao.getTodayVisitsNumber(today);

           Integer thisWeekOrderNumber= orderDao.getThisWeekOrderNumber(weekMonday ,weekSunday);

           Integer thisWeekVisitsNumber= orderDao.getThisWeekVisitsNumber(weekMonday ,weekSunday);

           Integer thisMonthOrderNumber= orderDao.getThisMonthOrderNumber(monthFirst ,monthLast);

           Integer thisMonthVisitsNumber= orderDao.getThisMonthVisitsNumber(monthFirst ,monthLast);

           List<Map<String,Object>> hotSetmeal = setmealDao.getHotSetmeal();

            map.put("todayNewMember",todayNewMember);
            map.put("totalMember",totalMember);
            map.put("thisWeekNewMember",thisWeekNewMember);
            map.put("thisMonthNewMember",thisMonthNewMember);
            map.put("todayOrderNumber",todayOrderNumber);
            map.put("todayVisitsNumber",todayVisitsNumber);
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
            map.put("hotSetmeal",hotSetmeal);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
//
//    @Override
//    public TravelGroup selectById(Integer id) {
//        return travelGroupDao.selectById(id);
//    }
//
//    @Override
//    public void editTravelGroup(TravelGroup travelGroup, Integer[] travelItemIds) {
//        travelGroupDao.editTravelGroup(travelGroup);
//
//        travelGroupDao.deleteTravelGroupAndItemById(travelGroup.getId());
//
//        if (travelItemIds.length > 0){
//
//            for (Integer travelItemId : travelItemIds) {
//
//                Map<String,Integer> map = new HashMap<>();
//
//                map.put("travelItemId",travelItemId);
//
//                map.put("travelGroupId",travelGroup.getId());
//
//                travelGroupDao.addTravelGroupAndItem(map);
//
//            }
//        }
//    }
//
//    @Override
//    public void deleteTravelItemById(Integer id) {
//        int count = travelGroupDao.selectTravelItemByItemId(id);
//        if (count > 0){
//            throw new RuntimeException("数据关联不可删除！");
//        }
//        travelGroupDao.deleteTravelItemById(id);
//    }
//
//    @Override
//    public List<TravelGroup> findAll() {
//        return travelGroupDao.findAll();
//    }
//
//    @Override
//    public void deleteTravelItemById(Integer id) {
//        travelItemDao.deleteTravelItemById(id);
//    }
//
//    @Override
//    public TravelItem selectById(Integer id) {
//        return travelItemDao.selectById(id);
//    }
//
//    @Override
//    public void editTravelItem(TravelItem travelItem) {
//        travelItemDao.editTravelItem(travelItem);
//    }
//
//    @Override
//    public List<TravelItem> selectTravelItemList() {
//        return travelItemDao.selectAllTravelItemList();
//    }
}
